//
//  HotProjectView.m
//  Aierle
//
//  Created by jiayibai-02por on 2017/6/1.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HotProjectView.h"
#import "HotStoreCell.h"

@interface HotProjectView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *hotStoreCollectionView;

@end

@implementation HotProjectView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews {

    UILabel *leftTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 100, 16)];
    leftTitleLabel.font = [UIFont systemFontOfSize:14];
    leftTitleLabel.textColor = [UIColor blackColor];
    leftTitleLabel.text = @"热门项目";
    [self addSubview:leftTitleLabel];
    
//    UIButton *rightWholeButton = [[UIButton alloc] initWithFrame:CGRectMake(Screen_width - YBKAdaptiveW(65), 0, YBKAdaptiveW(65), YBKAdaptiveH(35))];
//    [rightWholeButton setTitle:@"全部>" forState:UIControlStateNormal];
//    rightWholeButton.titleLabel.font = [UIFont systemFontOfSize:10];
//    [rightWholeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [rightWholeButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:rightWholeButton];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    layout.minimumLineSpacing = (Screen_width-YBKAdaptiveW(65)*4)/4.0;
    layout.minimumInteritemSpacing = 20;
    layout.itemSize = CGSizeMake(YBKAdaptiveW(65), YBKAdaptiveH(87));
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.hotStoreCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, YBKAdaptiveH(35), Screen_width, YBKAdaptiveH(87)) collectionViewLayout:layout];
    self.hotStoreCollectionView.delegate = self;
    self.hotStoreCollectionView.dataSource = self;
    self.hotStoreCollectionView.backgroundColor = [UIColor whiteColor];
    [self.hotStoreCollectionView registerNib:[UINib nibWithNibName:@"HotStoreCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HotStore"];
    self.hotStoreCollectionView.showsVerticalScrollIndicator = NO;
    self.hotStoreCollectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.hotStoreCollectionView];
}

- (void)setHotProjectArray:(NSArray *)hotProjectArray {

    _hotProjectArray = hotProjectArray;
    if (hotProjectArray.count != 0) {
        
        [self.hotStoreCollectionView reloadData];
    }
}

#pragma  mark --点击全部
- (void)rightButtonClick:(UIButton *)sender {

    
}

#pragma mark -- UICollectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.hotProjectArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifier = @"HotStore";
    HotStoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        
        cell = [[HotStoreCell alloc] initWithFrame:CGRectZero];
    }
    if (self.hotProjectArray.count != 0) {
        
        NSString *imageUrl = [NSString stringWithFormat:@"%@",[self.hotProjectArray[indexPath.item] objectForKey:@"logo"]];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil];
        NSString *cellString = [NSString stringWithFormat:@"%@",[self.hotProjectArray[indexPath.item] objectForKey:@"name"]];
        cell.nameLabel.text = cellString;
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.didClickCellBlock) {
        
        self.didClickCellBlock([self.hotProjectArray[indexPath.item] objectForKey:@"url"]);
    }
}


@end
